import { ref } from "vue";

export const useStatisticsTools = (
  fetchFunction,
  { defaultSort = "saleAmountDesc" } = {}
) => {
  const date = ref([]);
  const sort = ref(defaultSort);
  const loading = ref(false);
  const tableData = ref([]);
  const tableKey = ref(0);
  const tableDefaultSort = ref({
    prop: "saleAmount",
    order: "descending",
  });
  const changeDate = (v) => {
    date.value = v;
    getDataList();
  };
  const getDataList = () => {
    loading.value = true;
    const params = {
      startDate: date.value[0],
      endDate: date.value[1],
      sort: sort.value,
    };
    fetchFunction(params)
      .then((res) => {
        tableData.value = res.data;
        tableKey.value++;
      })
      .finally(() => {
        loading.value = false;
      });
  };

  const handleSortChange = ({ column, prop, order }) => {
    // 排序 saleAmountAsc销售额升序 saleAmountDesc销售额降序 saleCountAsc订单量升序 saleCountDesc订单量降序
    let sort = "";
    if (prop === "saleAmount") {
      sort = order === "ascending" ? "saleAmountAsc" : "saleAmountDesc";
    } else if (prop === "saleCount") {
      sort = order === "ascending" ? "saleCountAsc" : "saleCountDesc";
    }
    if (type === 2) {
      sort2.value = sort;
      getTableData2();
    }
  };
  return {
    date,
    changeDate,
    getDataList,
    loading,
    tableData,
    tableDefaultSort,
    tableKey,
  };
};
